<template>
  <div class="page-header">
    <div
      v-if="showBack"
      class="page-header__back"
      @click="goBack"
    >
      <i class="el-icon-back" />
      <div
        v-if="backText"
        class="page-header__back-text"
      >{{ backText }}</div>
    </div>
    <div class="page-header__content">
      <slot name="title">
        <div class="page-header__title">
          <em-icon
            v-if="icon"
            :name="icon"
            color="#409EFF"
          />
          <h3>
            <span>{{ title }}</span>
            <slot name="desc">
              <small>{{ desc }}</small>
            </slot>
          </h3>
        </div>
      </slot>
      <slot />
    </div>
  </div>
</template>

<script>
export default {
  name: 'PageHeader',
  props: {
    icon: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    desc: {
      type: String,
      default: ''
    },
    backText: {
      type: String,
      default: '返回'
    },
    showBack: {
      type: Boolean,
      default: false
    }
  },
  inject: ['goBack'],
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, 'per-page': val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, 'per-page': this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style lang="scss">
.page-header {
  display: flex;
  margin-bottom: 20px;
  &__back {
    display: flex;
    cursor: pointer;
    align-items: center;
    margin-right: 40px;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      width: 1px;
      height: 16px;
      right: -20px;
      top: 50%;
      transform: translateY(-50%);
      background-color: #dcdfe6;
    }
    .el-icon-back {
      font-size: 18px;
      margin-right: 6px;
    }
    &-text {
      font-size: 14px;
      font-weight: 500;
    }
  }
  &__content {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  &__title {
    display: flex;
    align-items: center;
    .em-icon {
      font-size: 21px;
      margin-right: 10px;
    }
    h3 {
      margin: 0;
      small {
        font-size: 12px;
        color: #ccc;
        margin-left: 10px;
      }
    }
  }
}
</style>
